package com.topcheer.gateway.ribbon;

import com.topcheer.gateway.ribbon.context.RibbonFilterContextHolder;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author xutao
 * @Package com.topcheer.gateway.ribbon
 * @Description: 限流拦截器
 * @date 2020/9/27 8:49
 */
public class RateLimiterFilter implements GatewayFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String path = exchange.getRequest().getURI().getPath().trim();
        // TODO 根据规则限流
        RibbonFilterContextHolder.getCurrentContext().put("path", path);
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
